package com.xiaoyy.app.extend.util;

import cn.hutool.setting.dialect.Props;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.commnetsoft.proxy.SsoClient;
import com.commnetsoft.proxy.model.CallResult;
import com.commnetsoft.proxy.model.UserInfo;
import com.xiaoyy.app.extend.common.vo.ZwwFrUserResVO;
import com.xiaoyy.core.common.exception.AppException;
import com.xiaoyy.core.common.vo.LoginVO;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;


public class ZjzzwUtil {

    /**
     * 获取政务网个人用户信息
     */
    public static LoginVO getZwUserInfo(LoginVO loginVO, HttpServletRequest request) {
        SsoClient client = SsoClient.getInstance();//单点登录工具
        //client.initConfig("test", "zjgwypwd", "http://puser.zjzwfw.gov.cn/sso/");
        //登录认证
        CallResult cr = client.login(request, loginVO.getLoginKey());
        if("0".equals(cr.getResult())){//认证成功登录系统
            //单点登录成功后，接入应用系统可根据自身业务需求需求，选择是否获取用户信息
            UserInfo user = client.getUser(request);
            if("0".equals(cr.getResult())){
                loginVO.setOpenId(user.getUserid());//第三方登录userid
                loginVO.setUserName(user.getLoginname());//登录名
                loginVO.setFullName(user.getUsername());//姓名
                loginVO.setUserType("20");//用户类型
                loginVO.setCardType(user.getIdtype());//证件类型
                loginVO.setIdcard(user.getIdnum());//证件号码
                loginVO.setEmail(user.getEmail());//邮箱
                loginVO.setCellphone(user.getMobile());//手机号
            }else{
                throw new AppException("获取政务网用户信息失败！");
            }

        }else{//认证失败

            throw new AppException("政务网登录认证失败！"+cr.getErrmsg());
        }
        return loginVO;
    }

    /**
     * 获取政务网法人用户信息
     */
    public static ZwwFrUserResVO getZwFrInfo(LoginVO loginVO, HttpServletRequest request,String frUrl,String projectId,String projectSecret) {
        JSONObject result=doQuery(loginVO.getLoginKey(),frUrl,projectId,projectSecret);//传入ssotoken
        ZwwFrUserResVO frUser=new ZwwFrUserResVO();
        if (null != result) {
            int errCode = result.getInteger("errCode");
            String msg = result.getString("msg");
            if (0 == errCode) {
                /* 用户信息 */
                String info = result.getString("info");
                System.out.println("用户信息  = " + info);
                frUser=JSON.parseObject(info, ZwwFrUserResVO.class);
            } else {
                throw new AppException("获取政务网法人用户信息失败:" + msg);
            }
        }
        return frUser;
    }


    /***
     * 验证令牌并获取用户的登录信息
     *
     * @param ssotoken
     */
    public static JSONObject doQuery(String ssotoken,String frUrl,String projectId,String projectSecret) {
        JSONObject param = new JSONObject();
        param.put("token", ssotoken);

        // 获取请求签名值
        String signature = DigestHelper.getSignature(param.toString(), projectSecret);
        String ContentType="application/json";
        String encoding="UTF-8";

        // 设置请求的Headers头信息
        LinkedHashMap<String, String> headers = HttpHelper.getPOSTHeaders(projectId,signature,ContentType,encoding);
        JSONObject jsonObj = HttpHelper.sendPOST(frUrl, param.toString(), headers,encoding);
        return jsonObj;
    }



}
